/**
 * 
 * Copyright (C) 2015 by Elcom Software Company
 *
 * @id: TestApi.java, May 8, 2015
 *
 */
package com.nip.fblike.core;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import com.nip.fblike.grapapi.FBCallGrapAPI;
import com.nip.fblike.grapapi.IFBLike;
import com.nip.fblike.grapapi.api.FBObjectLikes;
import com.nip.fblike.grapapi.page.FBPageFeed;
import com.nip.fblike.graphobject.PBObjectBase;
import com.nip.fblike.graphobject.page.FBObjectLikeObject;
import com.nip.fblike.graphobject.page.FBPageFeedObject;
import com.nip.fblike.graphobject.page.FBPostObject;
import com.nip.fblike.objects.UserLikes;

/**
 * @author ThangNM<thangnm@elcom.com.vn>
 *
 * @version v
 */
public class TestApi {

    private static long count;
    private static Set<UserLikes> liUserLikes = new HashSet<UserLikes>();
    private static IFBLike delegate = new IFBLike() {

        @Override
        public void done(PBObjectBase object) {
            // TODO Auto-generated method stub
            FBPageFeedObject objectRs = (FBPageFeedObject) object;
            System.out.println(objectRs.getListPosts().size());

            for (FBPostObject post : objectRs.getListPosts()) {
                FBObjectLikes likes = new FBObjectLikes(delegateLikes);
                likes.setUid(post.getId());
                FBCallGrapAPI.instance().call(likes);
            }
        }
    };

    private static IFBLike delegateLikes = new IFBLike() {

        @Override
        public synchronized void done(PBObjectBase jsonObject) {
            // TODO Auto-generated method stub
            FBObjectLikeObject object = (FBObjectLikeObject) jsonObject;
            System.out.println(object.getId() + ":"
                    + object.getListUserLike().size());
            count += object.getListUserLike().size();
            System.out.println("Count " + count);
            liUserLikes.addAll(object.getListUserLike());
            if(liUserLikes.size() >= 1000)
            {
                try {
                    write(liUserLikes);
                    liUserLikes.clear();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };

    public static void main(String[] args) {
        FBPageFeed api = new FBPageFeed(delegate);
        api.setUid("ChatVLcom");
        
//        FBObjectLikes api = new FBObjectLikes(delegateLikes);
//        api.setUid("1415575618657687_1623713017843945");
        
        FBCallGrapAPI.instance().call(api);
        System.out.println("Start read fan");
    }
    
    private static void write(Set<UserLikes> listUser) throws IOException
    {
        FileWriter file = new FileWriter("user"+System.currentTimeMillis()+".txt");
        
        for(UserLikes user : listUser)
        {
            file.write(user.getId()+"\n");
        }
        
        file.flush();
        file.close();
        
    }
}
